<?php

/**
 * @Project NUKEVIET 3.x
 * @Author VINADES.,JSC (contact@vinades.vn)
 * @Copyright (C) 2012 VINADES.,JSC. All rights reserved
 * @Createdate 2-9-2010 14:43
 */

if( ! defined( 'NV_IS_FILE_LANG' ) ) die( 'Stop!!!' );

$page_title = $lang_module['nv_lang_check'];

$xtpl = new XTemplate( "check.tpl", NV_ROOTDIR . "/themes/" . $global_config['module_theme'] . "/modules/" . $module_file );
$xtpl->assign( 'LANG', $lang_module );
$xtpl->assign( 'GLANG', $lang_global );

$array_lang_exit = array();

$result = $db->sql_query( "SHOW COLUMNS FROM `" . NV_LANGUAGE_GLOBALTABLE . "_file`" );

$add_field = true;
while( $row = $db->sql_fetch_assoc( $result ) )
{
	if( substr( $row['Field'], 0, 7 ) == "author_" )
	{
		$array_lang_exit[] .= trim( substr( $row['Field'], 7, 2 ) );
	}
}

if( empty( $array_lang_exit ) )
{
	$xtpl->assign( 'URL', NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name . "&amp;" . NV_OP_VARIABLE . "=setting" );

	$xtpl->parse( 'empty' );
	$contents = $xtpl->text( 'empty' );

	include ( NV_ROOTDIR . "/includes/header.php" );
	echo nv_admin_theme( $contents );
	include ( NV_ROOTDIR . "/includes/footer.php" );
	exit();
}
$lang_array_file = array();

$lang_array_file_temp = nv_scandir( NV_ROOTDIR . "/language", "/^[a-z]{2}+$/" );
foreach( $lang_array_file_temp as $value )
{
	if( file_exists( NV_ROOTDIR . "/language/" . $value . "/global.php" ) )
	{
		$lang_array_file[] = $value;
	}
}

$language_array_source = array( "vi", "en" );

$language_check_type = array(
	0 => $lang_module['nv_check_type_0'],
	1 => $lang_module['nv_check_type_1'],
	2 => $lang_module['nv_check_type_2']
);

$typelang = filter_text_input( 'typelang', 'post,get', '' );
$sourcelang = filter_text_input( 'sourcelang', 'post,get', '' );

$idfile = $nv_Request->get_int( 'idfile', 'post,get', 0 );
$check_type = $nv_Request->get_int( 'check_type', 'post,get', 0 );

if( $nv_Request->isset_request( 'idfile,savedata', 'post' ) and $nv_Request->get_string( 'savedata', 'post' ) == md5( $global_config['sitekey'] . session_id() ) )
{
	$pozlang = $nv_Request->get_array( 'pozlang', 'post', array() );

	if( ! empty( $pozlang ) )
	{
		foreach( $pozlang as $id => $lang_value )
		{
			$id = intval( $id );
			$lang_value = trim( strip_tags( $lang_value, NV_ALLOWED_HTML_LANG ) );
		
			if( ! empty( $lang_value ) )
			{
				$db->sql_query( "UPDATE `" . NV_LANGUAGE_GLOBALTABLE . "` SET `lang_" . $typelang . "`='" . mysql_real_escape_string( $lang_value ) . "' WHERE `id`='" . $id . "'" );
			}
		}
	}
}
$array_files = array();

$xtpl->assign( 'NV_BASE_ADMINURL', NV_BASE_ADMINURL );
$xtpl->assign( 'NV_NAME_VARIABLE', NV_NAME_VARIABLE );
$xtpl->assign( 'NV_OP_VARIABLE', NV_OP_VARIABLE );

$xtpl->assign( 'MODULE_NAME', $module_name );
$xtpl->assign( 'OP', $op );

foreach( $language_array as $key => $value )
{
	if( in_array( $key, $array_lang_exit ) )
	{
		$xtpl->assign( 'LANGUAGE', array( 'key' => $key, 'selected' => ( $key == $typelang ) ? ' selected="selected"' : '', 'title' => $value['name'] ) );

		$xtpl->parse( 'main.language' );
	}
}

foreach( $language_array_source as $key )
{
	if( in_array( $key, $array_lang_exit ) )
	{
		$xtpl->assign( 'LANGUAGE_SOURCE', array( 'key' => $key, 'selected' => ( $key == $sourcelang ) ? ' selected="selected"' : '', 'title' => $language_array[$key]['name'] ) );

		$xtpl->parse( 'main.language_source' );
	}
}

$sql = "SELECT `idfile`, `module`, `admin_file` FROM `" . NV_LANGUAGE_GLOBALTABLE . "_file` ORDER BY `idfile` ASC";
$result = $db->sql_query( $sql );

while( list( $idfile_i, $module, $admin_file, ) = $db->sql_fetchrow( $result ) )
{
	switch( $admin_file )
	{
		case '1':
			$langsitename = $lang_module['nv_lang_admin'];
			break;
		case '0':
			$langsitename = $lang_module['nv_lang_site'];
			break;
		default:
			$langsitename = $admin_file;
			break;
	}
		
	$xtpl->assign( 'LANGUAGE_AREA', array( 'key' => $idfile_i, 'selected' => ( $idfile_i == $idfile ) ? ' selected="selected"' : '', 'title' => $module . " " . $langsitename ) );

	$xtpl->parse( 'main.language_area' );
	$array_files[$idfile_i] = $module . " " . $langsitename;
}

foreach( $language_check_type as $key => $value )
{
	$xtpl->assign( 'LANGUAGE_CHECK_TYPE', array( 'key' => $key, 'selected' => ( $key == $check_type ) ? ' selected="selected"' : '', 'title' => $value ) );

	$xtpl->parse( 'main.language_check_type' );
}

$submit = $nv_Request->get_int( 'submit', 'post,get', 0 );

if( $submit > 0 and in_array( $sourcelang, $array_lang_exit ) and in_array( $typelang, $array_lang_exit ) )
{
	$array_where = array();
	if( $idfile > 0 )
	{
		$array_where[] = "`idfile`='" . $idfile . "'";
	}

	if( $check_type == 0 )
	{
		$array_where[] = "`lang_" . $typelang . "`=''";
	}
	elseif( $check_type == 1 )
	{
		$array_where[] = "`lang_" . $typelang . "`=`lang_" . $sourcelang . "`";
	}

	if( empty( $array_where ) )
	{
		$query = "SELECT `id`, `idfile`, `lang_key`, `lang_" . $typelang . "` as datalang, `lang_" . $sourcelang . "` as sourcelang FROM `" . NV_LANGUAGE_GLOBALTABLE . "` ORDER BY `id` ASC";
	}
	else
	{
		$query = "SELECT `id`, `idfile`, `lang_key`, `lang_" . $typelang . "` as datalang, `lang_" . $sourcelang . "` as sourcelang FROM `" . NV_LANGUAGE_GLOBALTABLE . "` WHERE " . implode( " AND ", $array_where ) . " ORDER BY `id` ASC";
	}
	$result = $db->sql_query( $query );

	$array_lang_data = array();

	while( list( $id, $idfile_i, $lang_key, $datalang, $datasourcelang ) = $db->sql_fetchrow( $result ) )
	{
		$array_lang_data[$idfile_i][$id] = array(
			'lang_key' => $lang_key,
			'datalang' => $datalang,
			'sourcelang' => $datasourcelang
		);
	}
	
	if( ! empty( $array_lang_data ) )
	{	
		$xtpl->assign( 'DATA', array(
			'typelang' => $typelang,
			'sourcelang' => $sourcelang,
			'check_type' => $check_type,
			'idfile' => $idfile,
			'savedata' => md5( $global_config['sitekey'] . session_id() )
		) );
	
		foreach( $array_lang_data as $idfile_i => $array_lang_file )
		{
			$xtpl->assign( 'CAPTION', $array_files[$idfile_i] );
			
			foreach( $array_lang_file as $id => $row )
			{				
				$xtpl->assign( 'ROW', array(
					'class' => ( ++ $i % 2 ) ? " class=\"second\"" : "",
					'stt' => $i,
					'lang_key' => $row['lang_key'],
					'datalang' => nv_htmlspecialchars( $row['datalang'] ),
					'id' => $id,
					'sourcelang' => nv_htmlspecialchars( $row['sourcelang'] ),
				) );
				
				$xtpl->parse( 'main.data.lang.loop' );
			}
			
			$xtpl->parse( 'main.data.lang' );
		}
		
		$xtpl->parse( 'main.data' );
	}
	else
	{
		$xtpl->parse( 'main.nodata' );
	}
	
	unset( $array_lang_data, $array_files );
}

$xtpl->parse( 'main' );
$contents = $xtpl->text( 'main' );

include ( NV_ROOTDIR . "/includes/header.php" );
echo nv_admin_theme( $contents );
include ( NV_ROOTDIR . "/includes/footer.php" );

?>